
<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <meta charset="utf-8"></meta>
      <meta name="viewport" content="width=device-width, initial-scale=1.0"></meta>
      <title>14.2.&nbsp;Virtual Nodes/Rels - Chapter&nbsp;14.&nbsp;Virtual Nodes &amp; Relationships (Graph Projections)</title>
      <link rel="stylesheet" type="text/css" href="../../docbook.css"></link>
      <link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.1.0/css/font-awesome.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans:400,300,400italic,600,300italic"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.css"></link>
      <link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/theme/neo.min.css"></link>
      <link rel="stylesheet" type="text/css" href="../../css/chunked-base.css"></link>
      <link rel="stylesheet" type="text/css" href="../../css/extra.css"></link><script src="//code.jquery.com/jquery-1.12.4.js" type="text/javascript"></script><script src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js" type="text/javascript"></script><script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/codemirror.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/addon/runmode/runmode.min.js" type="text/javascript"></script><script src="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.11.0/mode/cypher/cypher.min.js" type="text/javascript"></script><script src="../../javascript/datatable.js" type="text/javascript"></script><script src="../../javascript/colorize.js" type="text/javascript"></script><script src="../../javascript/tabs-for-chunked.js" type="text/javascript"></script><script src="../../javascript/mp-nav.js" type="text/javascript"></script><script src="../../javascript/versionswitcher.js" type="text/javascript"></script><script src="../../javascript/version.js" type="text/javascript"></script><script src="//s3-eu-west-1.amazonaws.com/alpha.neohq.net/docs/new-manual/assets/search.js" type="text/javascript"></script><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"></meta>
      <link rel="prev" href="../" title="Chapter&nbsp;14.&nbsp;Virtual Nodes &amp; Relationships (Graph Projections)"></link>
      <link rel="next" href="../virtual-graph/" title="14.5.&nbsp;Virtual Graph"></link>
      <link rel="shortcut icon" href="https://neo4j.com/wp-content/themes/neo4jweb/favicon.ico"></link><script>
        $(document).ready(function() {
          CodeMirror.colorize();
          tabTheSource($('body'));
          var $header = $('header').first();
          $header.prepend(
            $('<a href="" id="logo"><img src="https://neo4j.com/wp-content/themes/neo4jweb/assets/images/neo4j-logo-2015.png" alt="Neo4j Logo"></img></a>')
          );
          var $sidebar = $('<div id="sidebar-wrapper"></div>');
          $.get('toc.html', function (d){
            $(d).appendTo($sidebar);
            highlightToc();
            highlightLibraryHeader();
          });
          $sidebar.insertAfter($('header').first());
        });
        </script></head>
   <body>
      <header>
         <div class="searchbox">
            <form id="search-form" class="search" name="search-form" role="search"><input id="search-form-input" name="q" title="search" type="search" lang="en" placeholder="Search Neo4j docs..." aria-label="Search Neo4j documentation" max-length="128" required="required"></input><input id="search-form-button" type="submit" value="Search"></input></form>
         </div>
         <ul class="documentation-library">
            <li><a href="https://neo4j.com/docs/operations-manual/current">Operations Manual</a></li>
            <li><a href="https://neo4j.com/docs/developer-manual/current/">Developer Manual</a></li>
            <li><a href="https://neo4j.com/docs/ogm-manual/current/">OGM Manual</a></li>
            <li><a href="https://neo4j.com/docs/graph-algorithms/current/">Graph Algorithms</a></li>
            <li><a href="https://neo4j-contrib.github.io/neo4j-apoc-procedures/3.4/">APOC</a></li>
            <li><a href="https://neo4j.com/docs/java-reference/current/">Java Reference</a></li>
         </ul>
         <nav id="header-nav"><span class="nav-previous"><a accesskey="p" href="../"><span class="fa fa-long-arrow-left" aria-hidden="true"></span>Virtual Nodes &amp; Relationships (Graph Projections)</a></span><span class="nav-current">
               <p class="nav-title hidden">14.2.&nbsp;Virtual Nodes/Rels</p></span><span class="nav-next"><a accesskey="n" href="../virtual-graph/">Virtual Graph<span class="fa fa-long-arrow-right" aria-hidden="true"></span></a></span></nav>
      </header>
      <div id="search-results" class="hidden"></div>
      <section class="section" id="virtual-nodes-rels">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title" style="clear: both"><a class="anchor" href="#virtual-nodes-rels"></a>14.2.&nbsp;Virtual Nodes/Rels
                  </h2>
               </div>
            </div>
         </div>
         <p>Virtual Nodes and Relationships don&#8217;t exist in the graph, they are only returned to the UI/user for representing a graph projection.
            They can be visualized or processed otherwise.
            Please note that they have negative id&#8217;s.
         </p>
         <section class="section" id="_function_overview">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a class="anchor" href="#_function_overview"></a>14.2.1.&nbsp;Function Overview
                     </h3>
                  </div>
               </div>
            </div>
            <div class="informaltable">
               <div class="table" id="d0e17525">
                  <table class="informaltable" border="1">
                     <colgroup>
                        <col class="col_1"></col>
                        <col class="col_2"></col>
                     </colgroup>
                     <tbody>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.create.vNode(['Label'], {key:value,&#8230;&#8203;}) YIELD node</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>returns a virtual node</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.create.vNode(['Label'], {key:value,&#8230;&#8203;})</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>function returns a virtual node</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.create.vNodes(['Label'], [{key:value,&#8230;&#8203;}])</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>returns virtual nodes</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.create.vRelationship(nodeFrom,'KNOWS',{key:value,&#8230;&#8203;}, nodeTo) YIELD rel</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>returns a virtual relationship</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">apoc.create.vRelationship(nodeFrom,'KNOWS',{key:value,&#8230;&#8203;}, nodeTo)</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>function returns a virtual relationship</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.create.vPattern({_labels:['LabelA'],key:value},'KNOWS',{key:value,&#8230;&#8203;}, {_labels:['LabelB'],key:value})</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>returns a virtual pattern</p>
                           </td>
                        </tr>
                        <tr>
                           <td style="text-align: left; vertical-align: top; ">
                              <p><code class="literal">CALL apoc.create.vPatternFull(['LabelA'],{key:value},'KNOWS',{key:value,&#8230;&#8203;},['LabelB'],{key:value})</code></p>
                           </td>
                           <td style="text-align: left; vertical-align: top; ">
                              <p>returns a virtual pattern</p>
                           </td>
                        </tr>
                     </tbody>
                  </table>
               </div>
            </div>
         </section>
         <section class="section" id="_virtual_nodes_rels_example">
            <div class="titlepage">
               <div>
                  <div>
                     <h3 class="title"><a class="anchor" href="#_virtual_nodes_rels_example"></a>14.2.2.&nbsp;Virtual Nodes/Rels Example
                     </h3>
                  </div>
               </div>
            </div>
            <p><span class="formalpara-title">Simple example aggregate Relationships.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (from:Account)-[:SENT]-&gt;(p:Payment)-[:RECEIVED]-&gt;(to:Account)
RETURN from, to, apoc.create.vRelationship(from,'PAID',{amount:sum(p.amount)},to) as rel;</code></pre><p>
               
            </p>
            <p><span class="formalpara-title">Example with virtual node lookups, people grouped by their countries.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (p:Person) WITH collect(distinct p.country) as countries
WITH [cName IN countries | apoc.create.vNode(['Country'],{name:cName})] as countryNodes
WITH apoc.coll.groupBy(countryNodes,'name') as countries
MATCH (p1:Person)-[:KNOWS]-&gt;(p2:Person)
WITH p1.country as cFrom, p2.country as cTo, count(*) as count, countries
RETURN countries[cFrom] as from, countries[cTo] as to, apoc.create.vRelationship(countries[cFrom],'KNOWS',{count:count},countries[cTo]) as rel;</code></pre><p>
               
            </p>
            <p>That&#8217;s of course easier with <code class="literal">apoc.nodes.group</code>.
            </p>
            <p>From a simple dataset</p><pre class="programlisting highlight"><code data-lang="cypher">CREATE(a:Person)-[r:ACTED_IN]-&gt;(b:Movie)</code></pre><p>We can create a virtual copy, adding as attribute <code class="literal">name</code> the labels value
            </p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (a)-[r]-&gt;(b)
WITH head(labels(a)) AS l, head(labels(b)) AS l2, type(r) AS rel_type, count(*) as count
CALL apoc.create.vNode([l],{name:l}) yield node as a
CALL apoc.create.vNode([l2],{name:l2}) yield node as b
CALL apoc.create.vRelationship(a,rel_type,{count:count},b) yield rel
RETURN *;</code></pre><div class="informalfigure">
               <div class="mediaobject"><img src="https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/3.4/docs/images/apoc.create.vRelationshipAndvNode.png" width="800" alt="apoc.create.vRelationshipAndvNode"></img></div>
            </div>
            <div class="admonitionblock note">
               <table>
                  <tbody>
                     <tr>
                        <td class="icon"><i class="fa icon-note" title="note"></i></td>
                        <td class="content">
                           <p>Virtual nodes and virtual relationships have always a negative id</p>
                        </td>
                     </tr>
                  </tbody>
               </table>
            </div>
            <div class="informalfigure">
               <div class="mediaobject"><img src="https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/3.4/docs/images/vNodeId.png" width="200" alt="vNodeId"></img></div>
            </div>
            <p><span class="formalpara-title">Virtual pattern <code class="literal">vPattern</code>.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.create.vPattern({_labels:['Person'],name:'Mary'},'KNOWS',{since:2012},{_labels:['Person'],name:'Michael'})</code></pre><p>
               
            </p>
            <div class="informalfigure">
               <div class="mediaobject"><img src="https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/3.4/docs/images/apoc.create.vPattern.png" width="800" alt="apoc.create.vPattern"></img></div>
            </div>
            <p>We can add more labels, just adding them on <code class="literal">_labels</code></p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.create.vPattern({_labels:['Person', 'Woman'],name:'Mary'},'KNOWS',{since:2012},{_labels:['Person', 'Man'],name:'Michael'})</code></pre><div class="informalfigure">
               <div class="mediaobject"><img src="https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/3.4/docs/images/apoc.create.vPatternLabels.png" width="800" alt="apoc.create.vPatternLabels"></img></div>
            </div>
            <p><span class="formalpara-title">Virtual pattern full <code class="literal">vPatternFull</code>.&nbsp;</span>
               
            </p><pre class="programlisting highlight"><code data-lang="cypher">CALL apoc.create.vPatternFull(['British','Person'],{name:'James', age:28},'KNOWS',{since:2009},['Swedish','Person'],{name:'Daniel', age:30})</code></pre><p>
               
            </p>
            <div class="informalfigure">
               <div class="mediaobject"><img src="https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/3.4/docs/images/apoc.create.vPatternFull.png" width="800" alt="apoc.create.vPatternFull"></img></div>
            </div>
            <p>We can create a virtual pattern from an existing one</p><pre class="programlisting highlight"><code data-lang="cypher">CREATE(a:Person {name:'Daniel'})-[r:KNOWS]-&gt;(b:Person {name:'John'})</code></pre><p>From this dataset we can create a virtual pattern</p><pre class="programlisting highlight"><code data-lang="cypher">MATCH (a)-[r]-&gt;(b)
WITH head(labels(a)) AS labelA, head(labels(b)) AS labelB, type(r) AS rel_type, a.name AS aName, b.name AS bName
CALL apoc.create.vPatternFull([labelA],{name: aName},rel_type,{since:2009},[labelB],{name: bName}) yield from, rel, to
RETURN *;</code></pre><div class="informalfigure">
               <div class="mediaobject"><img src="https://raw.githubusercontent.com/neo4j-contrib/neo4j-apoc-procedures/3.4/docs/images/apoc.create.vPatternFullTwo.png" width="800" alt="apoc.create.vPatternFullTwo"></img></div>
            </div>
         </section>
      </section>
      <footer><script type="text/javascript">
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
          //Allow Linker
          ga('create', 'UA-1192232-34','auto', {'allowLinker': true});
          ga('send', 'pageview');
          // Load the plugin.
          ga('require', 'linker');
          // Define which domains to autoLink.
          ga('linker:autoLink', ['neo4j.org','neo4j.com','neotechnology.com','graphdatabases.com','graphconnect.com']);
        </script><script type="text/javascript">
          document.write(unescape("%3Cscript src='//munchkin.marketo.net/munchkin.js' type='text/javascript'%3E%3C/script%3E"));
        </script><script>Munchkin.init('773-GON-065');</script></footer>
   </body>
</html>